package com.xiaoy.core.remote.webservice.client.axis;

import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.ws.WebServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xiaoy.core.remote.webservice.client.KeyValue;
import com.xiaoy.core.remote.webservice.client.MyWebServerVO;
import com.xiaoy.core.remote.webservice.client.MyWebService;


@org.springframework.stereotype.Service("axisImpl")
public class AxisImpl implements MyWebService {

	protected Logger	logger	= LoggerFactory.getLogger(getClass());

	public String execution(MyWebServerVO vo) throws WebServiceException{

		try {
			List<KeyValue>kvs = vo.getParams();
			
			Object[] ps = new Object[kvs.size()];
			String serverUrl = vo.getServerUrl();
			String namespace = vo.getNamespace();

			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(serverUrl);
			call.setOperationName(new QName(namespace, vo.getMethod()));// WSDL里面描述的接口名称
			
			for (int i=0;i<kvs.size();i++) {
				
				KeyValue kv = kvs.get(i);
				call.addParameter(kv.getKey(), XMLType.XSD_STRING, ParameterMode.IN);// 接口的参数
				ps[i] = kv.getValue();
			}
			

			call.setReturnType(XMLType.XSD_INT);// 设置返回类型
//			for (String s : ps) {
//				System.err.println(s);
//			}
			int result = (Integer) call.invoke(ps);
			return result + "";
		} catch (Exception e) {
			logger.error("", e);
			throw new WebServiceException("读取webservice wsdl 错误！");
		}
	}
}
